CXX=g++
TEST_COUNTER=if [ $$? -ne 0 ] ; then \
			 let "n+=1" ; \
			 fi ;

SRC=$(wildcard src/*.cpp)
HEADERS=$(wildcard src/*.h)
STATIC_OBJ=$(SRC:%.cpp=bin/static/%.o)
DYNAMIC_OBJ=$(SRC:%.cpp=bin/dynamic/%.o)
TESTS=$(wildcard tests/*.cpp)
EXE=$(TESTS:%.cpp=bin/%.x)

libs: lib/libuv.a lib/libuv.so

lib/libuv.a: $(STATIC_OBJ)
	@mkdir -p lib
	ar rcs $@ $^
bin/static/src/%.o: src/%.cpp $(HEADERS)
	@mkdir -p bin/static/src
	$(CXX) -pthread -std=c++0x -c $< -o $@

lib/libuv.so: $(DYNAMIC_OBJ)
	@mkdir -p lib
	$(CXX) -pthread -shared -Wl,-soname,libuv.so -o $@ $^
bin/dynamic/src/%.o: src/%.cpp $(HEADERS)
	@mkdir -p bin/dynamic/src
	$(CXX) -pthread -std=c++0x -fPIC -g -c -Wall $< -o $@

test: $(EXE)
	@n=0 ; \
	$(foreach exec,$^,echo "Running $(exec)..." ; $(exec) ; $(TEST_COUNTER)) \
	echo "$$n tests failed"; exit $$n
bin/tests/%.x: tests/%.cpp lib/libuv.a
	@mkdir -p bin/tests
	$(CXX) -pthread -std=c++0x -static -I./src -L./lib  $< -o $@ -luv -lpthread

clean:
	rm -fr bin lib
